/*
 * > /*
 >  * INSANE - Interactive Structural Analysis Environment
 >  *
 >  * Copyright (C) 2003-2004
 >  * Universidade Federal de Minas Gerais
 >  * Escola de Engenharia
 >  * Departamento de Engenharia de Estruturas
 >  *
 >  * Author's email :     insane@dees.ufmg.br
 >  * Author's Website :   http://www.dees.ufmg.br/insane
 >  *
 >  * This program is free software; you can redistribute it and/or
 >  * modify it under the terms of the GNU General Public License
 >  * as published by the Free Software Foundation; either version 2
 >  * of the License, or any later version.
 >  *
 >  * This program is distributed in the hope that it will be useful,
 >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 >  * GNU General Public License for more details.
 >  *
 >  * You should have received a copy of the GNU General Public License
 >  * along with this program; if not, write to the Free Software
 >  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 > 1307, USA.
 *
 */
package br.ufmg.dees.insane.xyplot.xyplot_controller;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.IView;
import br.ufmg.dees.insane.draw.CaptionDraw;
import br.ufmg.dees.insane.draw.Draw;
import br.ufmg.dees.insane.draw.LineDraw;
import br.ufmg.dees.insane.draw.MarkDraw;
import br.ufmg.dees.insane.draw.SequenceDraw;
import br.ufmg.dees.insane.draw.StringDraw;
import br.ufmg.dees.insane.draw.XYPlotAxisDraw;
import br.ufmg.dees.insane.draw.XYTitleDraw;
import br.ufmg.dees.insane.util.IPoint3d;
import br.ufmg.dees.insane.xyplot.xyplot_model.Axis;
import br.ufmg.dees.insane.xyplot.xyplot_model.ISequence;
import br.ufmg.dees.insane.xyplot.xyplot_model.XYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.AxisViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.CaptionState;
import br.ufmg.dees.insane.xyplot.xyplot_view.SequenceViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.TitleViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotView;
import br.ufmg.dees.insane.xyplot.xyplot_view.XYPlotViewState;

/**
 * The geometric controller for this application.
 * @author  Penna, Samuel & Pitangueira, Roque
 * @since  april 2005
 */
public class XYPlotController implements Controller {
	
	private XYPlot model;
	private IView view;
	private ArrayList<Object> selections = new ArrayList<Object>();
	private ArrayList<Object> sequences = new ArrayList<Object>();
	private XYPlotAxisDraw xAxis;
	private XYPlotAxisDraw yAxis;
	private XYTitleDraw title;
	
//	************************************************************************************
	
	/**
	 * The constructor method xyplot_controller, inicializes xymodel
	 * @param model a model
	 * @see XYPlotInterface
	 */
	public XYPlotController(XYPlot model) {
		this.model = model;
	}
	/**
	 * The constructor method xyplot_controller, inicializes XYPlot, IView 
	 * @param model a XYPlot model
	 * @param vs a IView
	 * @see XYPlotInterface
	 */
	public XYPlotController(XYPlot model, IView vs) {
		this.model = model;
		this.view = vs;
	}
	
	/**
	 * The constructor method xyplot_controller, inicializes IView 
	 * @param vs a IView
	 * @see XYPlotInterface
	 */
	public XYPlotController(IView vs) {
		this.view = vs;
	}
	
	public XYPlotController() {
	}
	
//	**************************************************************************************
	
	/**
	 * The method set XYPlot model
	 * @param newModel  a new XYPlot model
	 * @uml.property  name="model"
	 */
	public void setModel(XYPlot newModel) {
		this.model = newModel;
	}
	
//	**************************************************************************************
	/**
	 * The method set XYPlotView 
	 * @param newView a new XYPlotView 
	 */
	public void setView(XYPlotView newView) {
		this.view = newView;
	}
	
//	**************************************************************************************
	
	public void draw(Graphics2D g) {
		Draw draw;
		g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
		view.getDrawingArea().setBackground(this.view.getViewState().bgcolor);
		
		Rectangle2D.Double lim = ((XYPlotViewState)this.view.getViewState()).getLimits();
		double xmin = lim.x;
		double ymin = lim.y;
		double xmax = lim.x + lim.width;
		double ymax = lim.y + lim.height;
		
		xmin = this.findGridMinMax().x;
		ymin = this.findGridMinMax().y;
		xmax = this.findGridMinMax().x + this.findGridMinMax().width;
		ymax = this.findGridMinMax().y + this.findGridMinMax().height;
		
//		********** Draws the plot title *****************************************************        
		double xCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition();
		double yCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition();
		
		this.title = new XYTitleDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreT, yCentreT).x,((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreT, yCentreT).y);
		this.title.setString((String)this.model.getTitle());
		this.title.setAngle(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getAngle());
		this.title.setFontType(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getTitleFont());
		this.title.setFontSize(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getTitleSize());
		this.title.setFontStyle(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getTitleStyle());
		draw = (Draw)this.title;
		g.setColor(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getColor());
		draw.draw(g);
		
		//********* Draws the plot grid ********************************************
		if(((XYPlotViewState)this.view.getViewState()).isGridOn())
		{
			g.setStroke(new BasicStroke((float)((XYPlotViewState)this.view.getViewState()).gridLineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
			lim = ((XYPlotViewState)this.view.getViewState()).getGridLimits();
			g.setColor(((XYPlotViewState)this.view.getViewState()).gridColor);
			
			//******* Draw vertical plot grid lines ****************************************
			double stepX = ((XYPlotViewState)this.view.getViewState()).getGridXInc();
			
			if(stepX==0){
				stepX = 0.01;
			}
			
			double x = xmin;
			while (x < xmax)
			{
				LineDraw line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(x, ymin),
						((XYPlotView)this.view).getDrawingArea().getViewPoint(x, ymax));
				line.draw(g);
				x += stepX;
			}
			
			//*********** Draw horizontal plot grid lines ***************************************
			double stepY = ((XYPlotViewState)this.view.getViewState()).getGridYInc();
			
			if(stepY==0){
				stepY = 0.01;
			}
			
			double y = ymin;
			while (y < ymax)
			{
				LineDraw line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, y), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, y));
				line.draw(g);
				y += stepY;
			}    
			
			// Draws the plot grid boundary
			LineDraw line;
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymax), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymax));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymax), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymin));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymin), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymin));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymin), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymax));
			line.draw(g);
			
		}
		
		//********** Draws the plot graphic boundary **************************************
		if(((XYPlotViewState)this.view.getViewState()).isBoundaryOn()){
			g.setStroke(new BasicStroke((float)((XYPlotViewState)this.view.getViewState()).boundaryLineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
			g.setColor(((XYPlotViewState)this.view.getViewState()).boundaryColor);
			LineDraw line;
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymax), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymax));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymax), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymin));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmax, ymin), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymin));
			line.draw(g);
			line = new LineDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymin), ((XYPlotView)this.view).getDrawingArea().getViewPoint(xmin, ymax));
			line.draw(g);
		}      
		
		//*********** Draws the plot X axis ***********************************************
		MarkDraw md = new MarkDraw();
		lim = ((XYPlotViewState)this.view.getViewState()).getLimits();
		g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));  
		if(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().isAxisOn()){
			g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
			xAxis.draw(g);
			
			// Draw the X axis marks //
			if(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().isMarksOn()){
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(((XYPlotViewState)this.view.getViewState()).getPrecision());
				double stepX = this.model.getAxis(0).getMainMark();
				double x = this.model.getAxis(0).getP1().getX();
				double y = this.model.getAxis(0).getP1().getY();
				if(stepX==0){
					stepX=Math.abs((Math.abs(this.model.getAxis(0).getP1().x)-Math.abs(this.model.getAxis(0).getP2().x))/10);
				}
				if(this.model.getAxis(0).getP1().getX()<0){
					if(x > this.model.getAxis(0).getP2().getX()){
						while (x > this.model.getAxis(0).getP2().getX()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.VLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
							String lb = "" + nf.format(x);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
							md.draw(g);
							x -= stepX;
						}
					}else if(x < this.model.getAxis(0).getP2().getX()){
						while (x < this.model.getAxis(0).getP2().getX()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.VLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
							String lb = "" + nf.format(x);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
							md.draw(g);
							x += stepX;
						}
					}
				}else if(this.model.getAxis(0).getP1().getX()>0){
					while (x < this.model.getAxis(0).getP2().getX()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.VLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
						String lb = "" + nf.format(x);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
						md.draw(g);
						x += stepX;
					}
				}
				
				if(this.model.getAxis(0).getP1().getX()==0){
					if(this.model.getAxis(0).getP2().getX()>0){
						while (x < this.model.getAxis(0).getP2().getX()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.VLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
							String lb = "" + nf.format(x);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
							md.draw(g);
							x += stepX;
						}
					}
					if(this.model.getAxis(0).getP2().getX()<0){
						while (x > this.model.getAxis(0).getP2().getX()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.VLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
							String lb = "" + nf.format(x);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
							md.draw(g);
							x -= stepX;
						}
					}
				}
				
				if(x>0&&this.model.getAxis(0).getP2().getX()<0){
					while (x > this.model.getAxis(0).getP2().getX()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.VLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
						String lb = "" + nf.format(x);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
						md.draw(g);
						x -= stepX;
					}
				}
				
				if(x<0&&this.model.getAxis(0).getP2().getX()>0){
					while (x < this.model.getAxis(0).getP2().getX()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.VLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
						String lb = "" + nf.format(x);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
						md.draw(g);
						x += stepX;
					}
				}
				
				double xe = this.model.getAxis(0).getP2().getX();
				double ye = this.model.getAxis(0).getP2().getY();
				g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
				Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(xe, ye);
				md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisColor());
				md.setLocation(loca);
				md.setType(MarkDraw.VLINE);
				md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkSize()));
				String lb = "" + nf.format(xe);
				md.setLabel(lb);
				md.setLabelOn(true);
				md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextFont());
				md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextPlace()));
				md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextStyle());
				md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getMarkTextSize());
				md.draw(g);
				
			}
			
			// drawing the X axis title //
			
			if(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().isTitleOn())
			{
				g.setColor(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getAxisTitleColor());
				double xCentreX = ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleXCenter();
				double xCentreY = ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleYCenter();
				Point locX = ((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreX, xCentreY);
				StringDraw strX = new StringDraw(locX);
				strX.setString((String)this.model.getAxis(0).getLabel());
				strX.setAngle(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleTextAngle());
				strX.setCentre(locX);
				strX.setLocation(locX);
				strX.setFont(((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleTextFont(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleTextSize(), ((XYPlotViewState)this.view.getViewState()).getXAxisViewState().getTitleTextStyle());
				draw = (Draw)strX;
				draw.draw(g);
			}
		}
		
//		************** Draws the plot Y axis *************************************************
		
		if(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().isAxisOn())
		{
			g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
			yAxis.draw(g);
			
			// Draw Y axis marks//
			
			if(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().isMarksOn())
			{
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(((XYPlotViewState)this.view.getViewState()).getPrecision());
				double stepY = this.model.getAxis(1).getMainMark();
				double x = this.model.getAxis(1).getP1().getX();
				double y = this.model.getAxis(1).getP1().getY();
				if(stepY==0){
					stepY=Math.abs((Math.abs(this.model.getAxis(1).getP1().y)-Math.abs(this.model.getAxis(1).getP2().y))/10);
				}
				
				if(this.model.getAxis(1).getP1().getY()<0){
					if(y > this.model.getAxis(1).getP2().getY()){
						while (y > this.model.getAxis(1).getP2().getY()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.HLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
							String lb = "" + nf.format(y);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
							md.draw(g);
							y -= stepY;
						}
					}else if(y < this.model.getAxis(1).getP2().getY()){
						while (y < this.model.getAxis(1).getP2().getY()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.HLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
							String lb = "" + nf.format(y);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
							md.draw(g);
							y += stepY;
						}
					}
				}else if(this.model.getAxis(1).getP1().getY()>0){
					while (y < this.model.getAxis(1).getP2().getY()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.HLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
						String lb = "" + nf.format(y);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
						md.draw(g);
						y += stepY;
					}
				}
				
				if(this.model.getAxis(1).getP1().getY()==0){
					if(this.model.getAxis(1).getP2().getY()>0){
						while (y < this.model.getAxis(1).getP2().getY()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.HLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
							String lb = "" + nf.format(y);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
							md.draw(g);
							y += stepY;
						}
					}
					if(this.model.getAxis(1).getP2().getY()<0){
						while (y > this.model.getAxis(1).getP2().getY()){
							g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
							md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
							md.setLocation(loca);
							md.setType(MarkDraw.HLINE);
							md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
							String lb = "" + nf.format(y);
							md.setLabel(lb);
							md.setLabelOn(true);
							md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
							md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
							md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
							md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
							md.draw(g);
							y -= stepY;
						}
					}
				}
				
				if(y>0&&this.model.getAxis(1).getP2().getY()<0){
					while (y > this.model.getAxis(1).getP2().getY()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.HLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
						String lb = "" + nf.format(y);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
						md.draw(g);
						y -= stepY;
					}
				}
				
				if(y<0&&this.model.getAxis(1).getP2().getY()>0){
					while (y < this.model.getAxis(1).getP2().getY()){
						g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(x, y);
						md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
						md.setLocation(loca);
						md.setType(MarkDraw.HLINE);
						md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
						String lb = "" + nf.format(y);
						md.setLabel(lb);
						md.setLabelOn(true);
						md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
						md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
						md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
						md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
						md.draw(g);
						y += stepY;
					}
				}
				
				double xe = this.model.getAxis(1).getP2().getX();
				double ye = this.model.getAxis(1).getP2().getY();
				g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
				Point loca = ((XYPlotView)this.view).getDrawingArea().getViewPoint(xe, ye);
				md.setLabelColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisColor());
				md.setLocation(loca);
				md.setType(MarkDraw.HLINE);
				md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkSize()));
				String lb = "" + nf.format(ye);
				md.setLabel(lb);
				md.setLabelOn(true);
				md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextFont());
				md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextPlace()));
				md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextStyle());
				md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getMarkTextSize());
				md.draw(g);
				
			}
			
			
			
			
			// drawing the Y axis title //
			
			if(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().isTitleOn())
			{
				g.setColor(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getAxisTitleColor());
				double yCentreX = ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleXCenter();
				double yCentreY = ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleYCenter();
				Point locY = ((XYPlotView)this.view).getDrawingArea().getViewPoint(yCentreX, yCentreY);
				StringDraw strY = new StringDraw(locY);
				strY.setString((String)this.model.getAxis(1).getLabel());
				strY.setAngle(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleTextAngle());
				strY.setCentre(locY);
				strY.setLocation(locY);
				strY.setFont(((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleTextFont(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleTextSize(), ((XYPlotViewState)this.view.getViewState()).getYAxisViewState().getTitleTextStyle());
				draw = (Draw)strY;
				draw.draw(g);
			}
		}
		
//		***************** Draws the sequences **********************************************
		
		g.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));  
		float width;
		//int jointStyle;
		float[] dash;
		BasicStroke[] gList = new BasicStroke[this.sequences.size()];
		ListIterator sqc = sequences.listIterator();
		int nc = 0;
		MarkDraw[] markCaption = new MarkDraw[this.sequences.size()];
		while (sqc.hasNext())
		{ 
			md = new MarkDraw();
			width = (float)((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getLineWidth();
			//jointStyle = (int)((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getJointStyle();
			dash = ((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getDash();
			if(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getLineType()==0){
				g.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,10f,null,0.0f));  
				gList[nc] = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,10f,null,0.0f);
			}else{
				g.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,10f,dash,0.0f));
				gList[nc] = (new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,10f,dash,0.0f));
			}
			draw = (Draw)sqc.next();
			g.setColor((Color)((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getLineColor());
			if(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).isLineOn()){
				CaptionDraw.isLineOn = true;
				draw.draw(g);
			}else{CaptionDraw.isLineOn = false;}
			ISequence sq = (ISequence)draw.getRepresents();
			if(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).isMarksOn())
			{
				NumberFormat nf = NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(((XYPlotViewState)this.view.getViewState()).getPrecision());
				
				for (int i = 0; i < sq.getNumberOfPoints(); i++)
				{
					g.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));  
					g.setColor((Color)((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkColor());
					IPoint3d pt = sq.getPoint(i);
					String lb = "(" + nf.format(pt.x) + "," + nf.format(pt.y)
					+ ")";
					Point locSeq = ((XYPlotView)this.view).getDrawingArea().getViewPoint(pt.x, pt.y);
					
					md.setType((byte)(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkType()+1));
					md.setSize(new Dimension(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkSize(), ((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkSize()));
					md.setLocation(locSeq);
					md.setLabelOn(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).isMarkTextOn());
					md.setLabel(lb);
					md.setLabelFont(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkTextFont());
					md.setLabelPlace((byte)(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkTextPlace()+1));
					md.setLabelStyle(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkTextStyle());
					md.setLabelSize(((XYPlotViewState)this.view.getViewState()).getSequenceState(nc).getMarkTextSize()); 
					
					md.draw(g);
				}
				CaptionDraw.isMarksOn = true;
				markCaption[nc] = md;
			}else{CaptionDraw.isMarksOn = false;}
			
			nc++;
		}
		
//		***************** Draws the caption **********************************************              
//		***************** Draws the caption **********************************************              
		if(((XYPlotViewState)this.view.getViewState()).getCaptionState().isCaptionOn()){
			
			String[] labelSequences = new String[this.sequences.size()];
			String[] labelFont = new String[this.sequences.size()];
			int[] labelSize = new int[this.sequences.size()];
			byte[] labelStyle = new byte[this.sequences.size()];
			//	Color titleColor = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitleColor();
			Color[] lineColor = new Color[this.sequences.size()];
			Color[] markColor = new Color[this.sequences.size()];
			Color[] seqColor  = new Color[this.sequences.size()];
			
			for(int i=0;i<this.sequences.size();i++){
				labelSequences[i] = (String)model.getSequence(i).getLabel();
				labelSize[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getTitleFontSize();
				labelFont[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getMarkTextFont();
				labelStyle[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getTitleStyle(); 
				lineColor[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getLineColor();
				markColor[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getMarkColor();
				seqColor[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getTitleColor();
			}
			
			Point locC = ((XYPlotView)this.view).getDrawingArea().getViewPoint(((XYPlotViewState)this.view.getViewState()).getCaptionState().getXPosition(),((XYPlotViewState)this.view.getViewState()).getCaptionState().getYPosition());
			CaptionDraw caption = new CaptionDraw(locC,"",this.sequences.size(),labelSequences,labelSize);
			caption.labelFont=labelFont;
			for(int i=0;i<this.sequences.size();i++){
				caption.mark[i] =  markCaption[i];
				caption.markSize[i] = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getMarkSize();
				caption.lineWidth[i] = (float)((XYPlotViewState)this.view.getViewState()).getSequenceState(i).getLineWidth();
			}
			
			caption.title = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitle();
			caption.titleFont = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitleFont();
			caption.titleSize = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitleSize();
			caption.titleStyle = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitleStyle();
			caption.titleColor = ((XYPlotViewState)this.view.getViewState()).getCaptionState().getTitleColor();
			caption.captionOn = ((XYPlotViewState)this.view.getViewState()).getCaptionState().isTitleOn();
			caption.lineColor = lineColor;
			caption.markColor = markColor; 
			caption.labelStyle = labelStyle;
			caption.labelColor = seqColor;
			caption.gList = gList;
			caption.setLocation(locC);
			
			//	float[] captionDash = new float[this.sequences.size()];
			caption.draw(g);
			
		}
//		***************** Draws the textBox **********************************************                    
		/*   g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));  
		 
		 double xPositonTextBox = ((XYPlotViewState)this.view.getViewState()).getTextBoxState().getXPosition();
		 double yPositonTextBox = ((XYPlotViewState)this.view.getViewState()).getTextBoxState().getYPosition();
		 Point locTextBox = ((XYPlotView)this.view).getDrawingArea().getViewPoint(xPositonTextBox, yPositonTextBox);
		 
		 textBox = new StringDraw();
		 
		 this.textBox.setString((String)(((XYPlotViewState)this.view.getViewState()).getTextBoxState().getText()));
		 this.textBox.setCentre(locTextBox);
		 this.textBox.setLocation(locTextBox);
		 this.textBox.setFontType(((XYPlotViewState)this.view.getViewState()).getTextBoxState().getTextFont());
		 this.textBox.setFontSize(((XYPlotViewState)this.view.getViewState()).getTextBoxState().getTextSize());
		 this.textBox.setFontStyle(((XYPlotViewState)this.view.getViewState()).getTextBoxState().getTextStyle());
		 
		 draw = (Draw)this.textBox;
		 g.setColor(((XYPlotViewState)this.view.getViewState()).getTextBoxState().getTextColor());
		 draw.draw(g);      
		 */
	}
//	**************************************************************************************
	public void fit() {
		// Calculating bounds
		Rectangle2D.Double limits = findMinMax();
		
		limits.x = limits.getMinX() -0.35*limits.width;
		limits.y = limits.getMinY() -0.1*limits.height;
		limits.width = limits.width*1.35;
		limits.height = limits.height*1.1;
		
		((XYPlotView)this.view).getDrawingArea().setZoom(limits);
	}
//	**************************************************************************************
	/**
	 * @return  Returns the model.
	 * @uml.property  name="model"
	 */
	public XYPlot getModel(){
		return model;
	}
	
	public void add(Object obj) {
		
	}
//	**************************************************************************************
	public boolean containsControl(Point2D po) {
		return false;
	}
//	**************************************************************************************
	public boolean contains(Point2D po) {
		return false;
	}
//	**************************************************************************************
	public boolean contains(Point po) {
		return false;
	}
//	**************************************************************************************
	public boolean contains(IPoint3d po) {
		return false;
	}
//	**************************************************************************************
	public Point pick(Point p) {
		return p;
	}
//	**************************************************************************************
	public void remove(Object obj) {
		
	}
//	**************************************************************************************
	public List removeSelection() {
		
		return null;
	}
//	**************************************************************************************
	public void removeAll() {
		
	}
//	**************************************************************************************
	public void select(Point p) {
		this.SelectXAxis(p);
		this.SelectYAxis(p);
	}
//	**************************************************************************************
	private void SelectXAxis(Point p) {
		int index;
		if(xAxis.contains(p))
		{
			if(xAxis.isSelected())
			{
				xAxis.setSelected(false);
				index = selections.indexOf(xAxis.getRepresents());
				selections.remove(index);
				return;
			}
			else
			{
				xAxis.setSelected(true);
				selections.add(xAxis.getRepresents());
				return;
			}
		}
	}
//	**************************************************************************************
	private void SelectYAxis(Point p) {
		
		int index;
		if(yAxis.contains(p))
		{
			if(yAxis.isSelected())
			{
				yAxis.setSelected(false);
				index = selections.indexOf(yAxis.getRepresents());
				selections.remove(index);
				return;
			}
			else
			{
				yAxis.setSelected(true);
				selections.add(yAxis.getRepresents());
				return;
			}
		}
	}
	
//	**************************************************************************************
	
	public void selectAll() {
		Draw draw;
		selections = new ArrayList<Object>();
		ListIterator sqc = sequences.listIterator();
		while (sqc.hasNext())
		{
			draw = (Draw)sqc.next();
			draw.setSelected(true);
			selections.add(draw.getRepresents());
		}
	}
//	**************************************************************************************
	public void unSelectAll() {
		Draw draw;
		ListIterator sqc = sequences.listIterator();
		while (sqc.hasNext())
		{
			draw = (Draw)sqc.next();
			draw.setSelected(false);
		}
	}
//	**************************************************************************************
	public ArrayList getSelection() {
		return selections;
	}
//	**************************************************************************************
	public void compose() {
		sequences = new ArrayList<Object>();
		ISequence s;
		Point p1;
		Point p2;
		
		// X Axis
		p1 = ((XYPlotView)this.view).getDrawingArea().getViewPoint(((Axis)(model.getAxisList().get(0))).getP1().getX(), ((Axis)(model.getAxisList().get(0))).getP1().getY());
		p2 = ((XYPlotView)this.view).getDrawingArea().getViewPoint(((Axis)(model.getAxisList().get(0))).getP2().getX(), ((Axis)(model.getAxisList().get(0))).getP2().getY());
		this.xAxis = new XYPlotAxisDraw(p1, p2, model.getAxisList().get(0));
		
		// Y Axis
		p1 = ((XYPlotView)this.view).getDrawingArea().getViewPoint(((Axis)(model.getAxisList().get(1))).getP1().getX(), ((Axis)(model.getAxisList().get(1))).getP1().getY());
		p2 = ((XYPlotView)this.view).getDrawingArea().getViewPoint(((Axis)(model.getAxisList().get(1))).getP2().getX(), ((Axis)(model.getAxisList().get(1))).getP2().getY());
		this.yAxis = new XYPlotAxisDraw(p1, p2, model.getAxisList().get(1));
		
		// Sequences
		ListIterator sq = model.getSequencesList().listIterator();
		while (sq.hasNext())
		{
			s = (ISequence)sq.next();
			int np = s.getNumberOfPoints();
			double[] x = new double[np];
			double[] y = new double[np];
			for (int i = 0; i < np; i++)
			{
				Point pi = ((XYPlotView)this.view).getDrawingArea().getViewPoint(s.getPoint(i).getX(), s.getPoint(i).getY());
				x[i] = pi.getX();
				y[i] = pi.getY();
			}
			sequences.add(new SequenceDraw(np, x, y, s));
		}
		
		// Title
		double xCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition();
		double yCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition();
		//Point locT = ((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreT, yCentreT);
		this.title = new XYTitleDraw(((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreT, yCentreT).x,((XYPlotView)this.view).getDrawingArea().getViewPoint(xCentreT, yCentreT).y);
		
	}
//	**************************************************************************************
	public Object get(Point2D po) {
		Point location = ((XYPlotView)this.view).getDrawingArea().getViewPoint(po);
		Draw draw;
		ListIterator iseq = sequences.listIterator();
		while (iseq.hasNext())
		{
			draw = (Draw)iseq.next();
			if(draw.contains(location))
			{
				return draw.getRepresents();
			}
		}
		return null;
	}
//	**************************************************************************************    
	public String getPointData(Point po) {
		Draw draw;
		ListIterator iseq = sequences.listIterator();
		while (iseq.hasNext())
		{
			draw = (Draw)iseq.next();
			if(draw.contains(po))
			{
				String s = "" + draw.getLabel() + "\n";
				s += this.xAxis.getLabel() + " = ";
				s += this.yAxis.getLabel() + " = ";
				return s;
			}
		}
		return null;
	}
//	**************************************************************************************
	public Object get() {
		return null;
	}
//	**************************************************************************************
	/** 
	 * The method check if the draw is selected
	 * @return true if there is any selected draw in the list.
	 * @return false if there is no selected draw in the list.
	 */
	public boolean isSelected() {
//		Draw draw;
//		ListIterator iseq = sequences.listIterator();
//		while (iseq.hasNext())
//		{
//		draw = (Draw)iseq.next();
//		if(draw.isSelected())
//		{
//		return true;
//		}
//		}
		return false;
	}
//	**************************************************************************************
	/** 
	 * The method find the max and minimum to x and y coordinates of the draw's points
	 * @return limits the draw's limits
	 */
	private Rectangle2D.Double findMinMax() {
		double xMin = this.model.getAxis(0).getP1().getX();
		double yMin = this.model.getAxis(0).getP1().getY();
		double xMax = 0;
		double yMax = 0;
		
		// looking for in the sequences
		ISequence s;
		ListIterator sq = model.getSequencesList().listIterator();
		
		while (sq.hasNext())
		{
			s = (ISequence)sq.next();
			int np = s.getNumberOfPoints();
			for (int i = 0; i < np; i++)
			{
				
				if(s.getPoint(i).getX() <= xMin)
					xMin = s.getPoint(i).getX();
				if(s.getPoint(i).getY() <= yMin)
					yMin = s.getPoint(i).getY();
				if(s.getPoint(i).getX() >= xMax)
					xMax = s.getPoint(i).getX();
				if(s.getPoint(i).getY() >= yMax)
					yMax = s.getPoint(i).getY();
			}
		}
		
		// looking for in the X axis
		if(this.model.getAxis(0).getP1().getX() <= xMin)
			xMin = this.model.getAxis(0).getP1().getX();
		if(this.model.getAxis(0).getP1().getY() <= yMin)
			yMin = this.model.getAxis(0).getP1().getY();
		if(this.model.getAxis(0).getP2().getX() >= xMax)
			xMax = this.model.getAxis(0).getP2().getX();
		if(this.model.getAxis(0).getP2().getY() >= yMax)
			yMax = this.model.getAxis(0).getP2().getY();
		
		// looking for in the Y axis
		if(this.model.getAxis(1).getP1().getX() <= xMin)
			xMin = this.model.getAxis(1).getP1().getX();
		if(this.model.getAxis(1).getP1().getY() <= yMin)
			yMin = this.model.getAxis(1).getP1().getY();
		if(this.model.getAxis(1).getP2().getX() >= xMax)
			xMax = this.model.getAxis(1).getP2().getX();
		if(this.model.getAxis(1).getP2().getY() >= yMax)
			yMax = this.model.getAxis(1).getP2().getY();
		
//		looking for in the model title
		if(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition()<= xMin)
			xMin = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition();
		if(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition()<= yMin)
			yMin = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition();
		if(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition() >= xMax)
			xMax = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition();
		if(((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition()>= yMax)
			yMax = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition();  
		
		if(yMax == yMin){
			yMin -= 0.1;
			yMax += 0.1;
		}
		if(xMax == xMin){
			xMin -= 0.1;
			xMax += 0.1;
		}
		
		Rectangle2D.Double limits = new Rectangle2D.Double(xMin, yMin, xMax , yMax );
		return limits;
		
	}// Enf of findMinMax()
	
	private Rectangle2D.Double findGridMinMax() {
		double xMin = this.model.getAxis(0).getP1().getX();
		double yMin = this.model.getAxis(0).getP1().getY();
		double xMax = 0.0;
		double yMax = 0.0;
		
		// looking for in the sequences
		ISequence s;
		ListIterator sq = model.getSequencesList().listIterator();
		while (sq.hasNext())
		{
			s = (ISequence)sq.next();
			int np = s.getNumberOfPoints();
			for (int i = 0; i < np; i++)
			{
				if(s.getPoint(i).getX() <= xMin)
					xMin = s.getPoint(i).getX();
				if(s.getPoint(i).getY() <= yMin)
					yMin = s.getPoint(i).getY();
				if(s.getPoint(i).getX() >= xMax)
					xMax = s.getPoint(i).getX();
				if(s.getPoint(i).getY() >= yMax)
					yMax = s.getPoint(i).getY();
			}
		}
		
		// looking for in the X axis
		if(this.model.getAxis(0).getP1().getX() <= xMin)
			xMin = this.model.getAxis(0).getP1().getX();
		if(this.model.getAxis(0).getP1().getY() <= yMin)
			yMin = this.model.getAxis(0).getP1().getY();
		if(this.model.getAxis(0).getP2().getX() >= xMax)
			xMax = this.model.getAxis(0).getP2().getX();
		if(this.model.getAxis(0).getP2().getY() >= yMax)
			yMax = this.model.getAxis(0).getP2().getY();
		
		// looking for in the Y axis
		if(this.model.getAxis(1).getP1().getX() <= xMin)
			xMin = this.model.getAxis(1).getP1().getX();
		if(this.model.getAxis(1).getP1().getY() <= yMin)
			yMin = this.model.getAxis(1).getP1().getY();
		if(this.model.getAxis(1).getP2().getX() >= xMax)
			xMax = this.model.getAxis(1).getP2().getX();
		if(this.model.getAxis(1).getP2().getY() >= yMax)
			yMax = this.model.getAxis(1).getP2().getY();
		
		if(yMax == yMin){
			yMin -= 0.1;
			yMax += 0.1;
		}
		if(xMax == xMin){
			xMin -= 0.1;
			xMax += 0.1;
		}
		
		Rectangle2D.Double limits = new Rectangle2D.Double(xMin, yMin, xMax - xMin, yMax - yMin);
		return limits;
		
	}// Enf of findMinMax()
//	**************************************************************************************
	private Rectangle2D.Double AxisPositions() {
		//Posicionando o titulo do eixo Default
		double xMin = this.model.getAxis(0).getP1().getX();
		double yMin = this.model.getAxis(0).getP1().getY();
		double xMax = 0.0;
		double yMax = 0.0;
		
		// looking for in the X axis
		if(this.model.getAxis(0).getP1().getX() <= xMin)
			xMin = this.model.getAxis(0).getP1().getX();
		if(this.model.getAxis(0).getP1().getY() <= yMin)
			yMin = this.model.getAxis(0).getP1().getY();
		if(this.model.getAxis(0).getP2().getX() >= xMax)
			xMax = this.model.getAxis(0).getP2().getX();
		if(this.model.getAxis(0).getP2().getY() >= yMax)
			yMax = this.model.getAxis(0).getP2().getY();
		
		// looking for in the Y axis
		if(this.model.getAxis(1).getP1().getX() <= xMin)
			xMin = this.model.getAxis(1).getP1().getX();
		if(this.model.getAxis(1).getP1().getY() <= yMin)
			yMin = this.model.getAxis(1).getP1().getY();
		if(this.model.getAxis(1).getP2().getX() >= xMax)
			xMax = this.model.getAxis(1).getP2().getX();
		if(this.model.getAxis(1).getP2().getY() >= yMax)
			yMax = this.model.getAxis(1).getP2().getY();
		
		Rectangle2D.Double limits = new Rectangle2D.Double(xMin, yMin, xMax - xMin, yMax - yMin);
		return limits;
	}
//	**************************************************************************************
	/**
	 * method to set the default plot view
	 */
	public void setDefaultView() {
		
		// get the plot limits
		Rectangle2D.Double limits = findMinMax();
		
		// Add some border
		Rectangle2D.Double lim = new Rectangle2D.Double(limits.x, limits.y, limits.width, limits.height);
		lim.x = limits.x - 0.2 * limits.width;
		lim.y = limits.y - 0.2 * limits.height;
		lim.width = 1.4 * limits.width;
		lim.height = 1.4 * limits.height;
		
		// sets the general view
		XYPlotViewState vs = ((XYPlotViewState)this.view.getViewState());
		vs.setLimits(lim);
		vs.setPrecision(4);
		vs.bgcolor = Color.WHITE;
		
		// sets the plot grid view
		vs.setGrid(true);
		vs.setGridLimits(new Rectangle2D.Double(this.model.getAxis(0).getP1().x, this.model.getAxis(1).getP1().y,
				this.model.getAxis(0).getP2().x-this.model.getAxis(0).getP1().x, 
				this.model.getAxis(1).getP2().y-this.model.getAxis(1).getP1().y));
		vs.gridColor = Color.lightGray;
		vs.setGridIncrements(this.model.getAxis(0).getMainMark(),this.model.getAxis(1).getMainMark());
		vs.setGridIncrements(vs.getGridXInc(), vs.getGridYInc());
		
		//********sets the plot title view
		if(this.model.getAxis(0).getP1().x>=0&&this.model.getAxis(0).getP2().x>0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition( (0.5*(this.model.getAxis(0).getP2().x-this.model.getAxis(0).getP1().x))   );
			
		}else if(this.model.getAxis(0).getP1().x>=0&&this.model.getAxis(0).getP2().x<0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}else if(this.model.getAxis(0).getP1().x<=0&&this.model.getAxis(0).getP2().x<0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}else if(this.model.getAxis(0).getP1().x<=0&&this.model.getAxis(0).getP2().x>0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}
		
		if(this.model.getAxis(0).getP1().x>0&&this.model.getAxis(0).getP2().x>=0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x-this.model.getAxis(0).getP1().x));
		}else if(this.model.getAxis(0).getP1().x>0&&this.model.getAxis(0).getP2().x<=0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}else if(this.model.getAxis(0).getP1().x<0&&this.model.getAxis(0).getP2().x<=0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}else if(this.model.getAxis(0).getP1().x<0&&this.model.getAxis(0).getP2().x>=0){
			((XYPlotViewState)this.view.getViewState()).getTitleViewState().setXCenterPosition(0.5*(this.model.getAxis(0).getP2().x+this.model.getAxis(0).getP1().x));
		}
		
		if(this.model.getAxis(1).getP1().y>this.model.getAxis(1).getP2().y){
			if(this.model.getAxis(1).getP1().y==0){
				((XYPlotViewState)this.view.getViewState()).getTitleViewState().setYCenterPosition(this.model.getAxis(1).getP1().y+
						0.1*Math.abs(this.model.getAxis(1).getP1().y-this.model.getAxis(1).getP2().y));
			}else{
				((XYPlotViewState)this.view.getViewState()).getTitleViewState().setYCenterPosition(this.model.getAxis(1).getP1().y);
			}
		}else if(this.model.getAxis(1).getP2().y>this.model.getAxis(1).getP1().y){
			if(this.model.getAxis(1).getP2().y==0){
				((XYPlotViewState)this.view.getViewState()).getTitleViewState().setYCenterPosition(this.model.getAxis(1).getP2().y+
						0.1*Math.abs(this.model.getAxis(1).getP2().y-this.model.getAxis(1).getP1().y));
			}else{
				((XYPlotViewState)this.view.getViewState()).getTitleViewState().setYCenterPosition(this.model.getAxis(1).getP2().y);
			}
		}
		
		double xCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getXCenterPosition();
		double yCentreT = ((XYPlotViewState)this.view.getViewState()).getTitleViewState().getYCenterPosition();
		
		TitleViewState tvs = ((XYPlotViewState)this.view.getViewState()).getTitleViewState();
		tvs.setTitleOn(true);
		tvs.setColor(Color.BLACK);
		tvs.setTitleFont("Arial");
		tvs.setTitleSize(25);
		tvs.setTitleStyle((byte)1);
		
		tvs.setXCenterPosition(xCentreT);
		tvs.setYCenterPosition(yCentreT*1.1);
		//********************************
		limits = this.AxisPositions();
		
		AxisViewState xvs = ((XYPlotViewState)this.view.getViewState()).getXAxisViewState();
		xvs.setAxisOn(true);
		xvs.setAxisColor(new Color(153,153,153));
		xvs.setAxisTitleColor(Color.BLACK);
		xvs.setTitleTextStyle((byte)1); 
		xvs.setMarkSize(5);
		xvs.setMarksOn(true);
		xvs.setMarkTextFont("Arial");
		
		
		if(this.model.getAxis(1).getP1().x==0&this.model.getAxis(1).getP2().x>0)
			xvs.setMarkTextPlace(MarkDraw.SOUTH);
		if(this.model.getAxis(1).getP1().x>0&this.model.getAxis(1).getP2().x==0)
			xvs.setMarkTextPlace(MarkDraw.SOUTH);
		if(this.model.getAxis(1).getP1().x>0&this.model.getAxis(1).getP2().x>0)
			xvs.setMarkTextPlace(MarkDraw.SOUTH);
		if(this.model.getAxis(1).getP1().x<0&this.model.getAxis(1).getP2().x>0)
			xvs.setMarkTextPlace(MarkDraw.SOUTH);
		
		if(this.model.getAxis(1).getP1().x==0&this.model.getAxis(1).getP2().x<0)
			xvs.setMarkTextPlace(MarkDraw.NORTH);
		if(this.model.getAxis(1).getP1().x<0&this.model.getAxis(1).getP2().x==0)
			xvs.setMarkTextPlace(MarkDraw.NORTH);
		if(this.model.getAxis(1).getP1().x>0&this.model.getAxis(1).getP2().x<0)
			xvs.setMarkTextPlace(MarkDraw.NORTH);
		if(this.model.getAxis(1).getP1().x>0&this.model.getAxis(1).getP2().x<0)
			xvs.setMarkTextPlace(MarkDraw.NORTH);
		
		xvs.setMarkTextSize(10);
		xvs.setMarkTextStyle((byte)1);
		xvs.setTitleTextAngle(0.0);
		xvs.setTitleXCenter(limits.x + limits.width / 2);
		xvs.setTitleYCenter(limits.y - 0.1 * limits.height);
		
		// sets the plot Y axis view
		AxisViewState yvs = ((XYPlotViewState)this.view.getViewState()).getYAxisViewState();
		yvs.setAxisOn(true);
		yvs.setAxisColor(new Color(153,153,153));
		yvs.setAxisTitleColor(Color.BLACK);
		yvs.setTitleTextStyle((byte)1); 
		yvs.setMarkSize(5);
		yvs.setMarksOn(true);
		yvs.setMarkTextFont("Arial");
		yvs.setMarkTextPlace(MarkDraw.WEST);
		yvs.setMarkTextSize(10);
		yvs.setMarkTextStyle((byte)1);
		yvs.setTitleTextAngle(Math.PI / 2);
		yvs.setTitleYCenter(limits.y + limits.height / 2);
		yvs.setTitleXCenter(limits.x - 0.1 * limits.width);
		
		// sets the sequences view
		int ns = model.getSequencesList().size();
		
		for (int i = 0; i < ns; i++)
		{
			SequenceViewState svs = ((XYPlotViewState)this.view.getViewState()).getSequenceState(i);
			
			if(i==0){
				svs.setLineColor(new Color(0,51,153));
				svs.setMarkColor(new Color(0,51,153));
				svs.setMarkType((byte)(0));
			}else if(i==1){
				svs.setLineColor(new Color(255,0,255));
				svs.setMarkColor(new Color(255,0,255));
				svs.setMarkType((byte)(1));
			}else if(i==2){
				svs.setLineColor(new Color(0,255,51));
				svs.setMarkColor(new Color(0,255,51));
				svs.setMarkType((byte)(2));
			}else if(i==3){
				svs.setLineColor(new Color(204,0,51));
				svs.setMarkColor(new Color(204,0,51));
				svs.setMarkType((byte)(3));
			}else if(i==4){
				svs.setLineColor(new Color(0,255,255));
				svs.setMarkColor(new Color(0,255,255));
				svs.setMarkType((byte)(4));
			}else if(i==5){
				svs.setLineColor(new Color(255,255,0));
				svs.setMarkColor(new Color(255,255,0));
				svs.setMarkType((byte)(5));
			}else if(i==6){
				svs.setLineColor(new Color(102,51,0));
				svs.setMarkColor(new Color(102,51,0));
				svs.setMarkType((byte)(6));
			}else if(((double)i/2)==0){
				svs.setLineColor(new Color(64,51,0));
				svs.setMarkColor(new Color(64,51,0));
				svs.setMarkType((byte)(7));
			}else if(((double)i/2)!=0){
				svs.setLineColor(new Color(51,64,51));
				svs.setMarkColor(new Color(51,64,51));
				svs.setMarkType((byte)(8));
			}else{
				svs.setLineColor(new Color(0,51,153));
				svs.setMarkColor(new Color(0,51,153));
				svs.setTitleColor(Color.BLACK);
				svs.setMarksOn(false);
				svs.setMarkSize(6);
				svs.setMarkTextOn(true);
				svs.setMarkTextFont("Arial");
				svs.setMarkTextPlace(MarkDraw.NORTH);
				svs.setMarkTextSize(0);
				svs.setMarkTextStyle((byte)1);
				svs.setMarkType((byte)(0));
			}
			
			svs.setMarkTextFont("Arial");
			svs.setTitleColor(Color.BLACK);
			
			
		}
		
		CaptionState cptvs = ((XYPlotViewState)this.view.getViewState()).getCaptionState();
		//Rectangle2D.Double caplim = ((XYPlotViewState)this.view.getViewState()).getGridLimits();
		/*  double xmin = caplim.x  this.findGridMinMax().x
		 double ymin = caplim.y;
		 double xmax = caplim.x + caplim.width;
		 double ymax = caplim.y + caplim.height;
		 */
		//double xmin = this.findGridMinMax().x;
		//double ymin = this.findGridMinMax().y;
		double xmax = this.findGridMinMax().x + this.findGridMinMax().width;
		double ymax = this.findGridMinMax().y + this.findGridMinMax().height;
		
		Point locCaption = ((XYPlotView)this.view).getDrawingArea().getViewPoint((int)xmax, (int)ymax);
		cptvs.setXPosition(locCaption.x);
		cptvs.setYPosition(locCaption.y);
		cptvs.setTitleColor(Color.BLACK);
		cptvs.setTitleStyle((byte)1);
		cptvs.isCaptionOn();
		this.view.getViewState().setLimits(this.findMinMax());
		this.fit();
		
	}
//	**************************************************************************************
	
}// end of this class
